package com.bb.blog.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitMqUtil {
    private static RabbitTemplate rabbitTemplate;
    private static Logger logger = LoggerFactory.getLogger(RabbitMqUtil.class);

    @Autowired
    public void setRedisTemplate(RabbitTemplate redisTemplate) {
        RabbitMqUtil.rabbitTemplate = redisTemplate;
    }


    public static void send(String exchangeName, String routeKey, Object payload) {
        String body = serialize(payload);
        byte[] bytes = body.getBytes();
        Message message = new Message(bytes, new MessageProperties());
        rabbitTemplate.send(exchangeName, routeKey, message);
    }


    public static <T> T deserialize(String text, Class<T> clazz) {
        return JSONObject.parseObject(text, clazz);
    }

    public static String serialize(Object object) {
        return JSONObject.toJSONString(object);
    }
}
